package InformationManagement;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.TableColumn;

import org.junit.Test;

public class InformationManagement {
	public static void main(String[] args) {
		JFrame jFrame = new JFrame();
		JPanel jPanel = new JPanel();
		jFrame.add(jPanel);
		jPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		// 创建button构件
		JButton students_button = new JButton("学生管理");
		JButton teachers_button = new JButton("教师管理");

		// 添加构件
		jPanel.add(teachers_button);
		jPanel.add(students_button);

		// students_buton点击事件
		students_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StudentsInformtionManagement();
			}
		});

		// teachers_buton点击事件
		teachers_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				TechersInformationManagement();

			}
		});

		//jFrame设置
		jFrame.setVisible(true);
		jFrame.setSize(300, 200);
		jFrame.setResizable(false);
		jFrame.setLocation(400, 400);
		jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
		jFrame.setTitle("信息管理系统");
	}

	//教师信息管理部分
	@Test
	protected static void TechersInformationManagement() {
		JFrame teachers_frame = new JFrame("教师管理");
		JPanel panel2 = new JPanel();

		panel2.setLayout(new GridLayout(3, 1));

		//
//		Student student = new Student("001", "zjx", "男", "21", "Java");
//		Student student2 = new Student("002", "向记者", "女", "22", "ui");
//		list.add(student2);
		
		
		//数据读入
		List<Teachers> teacherslist = new ArrayList<>();
		teacherslist = inputStudentStream();

		// Student columnName = new Student("ID","姓名","性别","年龄","班级");
		// 信息panel部分
		// String[] columnName = { "ID", "姓名", "性别", "年龄", "班级" };
		// Object[][] obj = { { "001", "zjx", "男", "21", "Java" }, { "002",
		// "向记者", "女", "22", "ui" } };
		JPanel information_panel = new JPanel();
		TeacherTableModel tableModel = new TeacherTableModel();
		tableModel.setData(teacherslist);
		JTable teachers_table = new JTable(tableModel);
		TableColumn column = null;
		int colunms = teachers_table.getColumnCount();
		for (int i = 0; i < colunms; i++) {
			column = teachers_table.getColumnModel().getColumn(i);
			column.setPreferredWidth(150);
		}
		teachers_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		JScrollPane scrollPane = new JScrollPane(teachers_table);
		scrollPane.setSize(500, 200);

		information_panel.add(scrollPane);
		panel2.add(scrollPane);

		// 添加、删除button_panel部分
		JPanel button_panel = new JPanel();
		JButton add_button = new JButton("添加");
		JButton del_button = new JButton("删除");
		button_panel.add(add_button);
		button_panel.add(del_button);
		panel2.add(button_panel);

		// add_button添加点击事件
		add_button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				add_TeacherInformation();

			}

			//添加按键点击事件
			@Test
			private void add_TeacherInformation() {
				JFrame add_frame = new JFrame("添加信息");
				JPanel add_panel = new JPanel();
				add_panel.setLayout(new GridLayout(6, 1));
				add_frame.add(add_panel);

				// id_panel
				JPanel id_panel = new JPanel();
				JLabel id_label = new JLabel("ID:");
				JTextField id_field = new JTextField(10);
				id_panel.add(id_label);
				id_panel.add(id_field);
				add_panel.add(id_panel);

				// name_panel
				JPanel name_panel = new JPanel();
				JLabel name_label = new JLabel("姓名:");
				JTextField name_field = new JTextField(10);
				name_panel.add(name_label);
				name_panel.add(name_field);
				add_panel.add(name_panel);

				// sex_panel
				JPanel sex_panel = new JPanel();
				JLabel sex_label = new JLabel("性别:");
				JTextField sex_field = new JTextField(10);
				sex_panel.add(sex_label);
				sex_panel.add(sex_field);
				add_panel.add(sex_panel);

				// age_panel
				JPanel age_panel = new JPanel();
				JLabel age_label = new JLabel("年龄:");
				JTextField age_field = new JTextField(10);
				age_panel.add(age_label);
				age_panel.add(age_field);
				add_panel.add(age_panel);

				// class_panel
				JPanel class_panel = new JPanel();
				JLabel class_label = new JLabel("班级:");
				JTextField class_field = new JTextField(10);
				class_panel.add(class_label);
				class_panel.add(class_field);
				add_panel.add(class_panel);

				// button_panel
				JPanel button_panel = new JPanel();
				JButton yes_button = new JButton("确定");
				JButton no_button = new JButton("取消");
				button_panel.add(yes_button);
				button_panel.add(no_button);
				add_panel.add(button_panel);
				
				//确定按键点击事件.把信息添加入list
				yes_button.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						String id = id_field.getText();
						String name = name_field.getText();
						String sex = sex_field.getText();
						String age = age_field.getText();
						String classname = class_field.getText();

						Teachers teachers = new Teachers(id, name, sex, age, classname);
						teacherslist.add(teachers);
						tableModel.setData(teacherslist);
						tableModel.fireTableDataChanged();
						add_frame.setVisible(false);
						add_frame.dispose();
						
						//数据写入ObjectOutputStream
						OutputStream(teacherslist);
						
		
					}

				
				});
				//取消按键点击事件
				no_button.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						add_frame.setVisible(false);
						add_frame.dispose();
					}
				});
				

				// add_frame设置
				add_frame.add(add_panel);
				add_frame.setVisible(true);
				add_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				add_frame.setSize(500, 400);
				add_frame.setResizable(true);
				add_frame.setLocation(400, 400);				

			}
		});
		//del_button添加点击事件
		del_button.addActionListener(new ActionListener() {
			int del_row;//记录删除的行号
			@Override
			public void actionPerformed(ActionEvent e) {
				teachers_table.setColumnSelectionAllowed(true);//允许选中table中的行
				//添加鼠标监听事件
				teachers_table.addMouseListener(new MouseListener() {
					
					@Override
					public void mouseReleased(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mousePressed(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseExited(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseEntered(MouseEvent e) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void mouseClicked(MouseEvent e) {
					del_row =teachers_table.rowAtPoint(e.getPoint());//获取点击所选中的要删除的行号
					//System.out.println(del_row);
						
					}
				});

						teacherslist.remove(del_row);
						tableModel.setData(teacherslist);
						tableModel.fireTableDataChanged();
						
					}
		});
		
		// frame设置部分
		teachers_frame.add(panel2);
		teachers_frame.setVisible(true);
		teachers_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		teachers_frame.setSize(500, 300);
		teachers_frame.setLocation(400, 400);

	}

	
	@SuppressWarnings("unchecked")
	private static List<Teachers> inputStudentStream() {
		// TODO Auto-generated method stub
		InputStream inputStream = null;
		ObjectInputStream objectInputStream = null;
		List<Teachers> list = new ArrayList<>();
		try {
			inputStream = new FileInputStream("Students");
			objectInputStream = new ObjectInputStream(inputStream);
			Object object = objectInputStream.readObject();
			//Student student = (Student) object;
			list = (ArrayList<Teachers>) object;
			//list.add(student);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return list;
	}
	private static void OutputStream(List<Teachers> list) {
		// TODO Auto-generated method stub
		ObjectOutputStream objectOutputStream  =null;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("Students");
			objectOutputStream = new ObjectOutputStream(fileOutputStream);
			objectOutputStream.writeObject(list);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			if (fileOutputStream  != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	}


	//学生信息管理部分
	protected static void StudentsInformtionManagement() {

	}
}
